package com.yvtian.pu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.ShopEntity;
import com.yvtian.inv.service.ShopService;
import com.yvtian.pu.pojo.PuApplyForEntity;
import com.yvtian.pu.pojo.PuGoodsReceiptEntity;
import com.yvtian.pu.dao.PuGoodsReceiptMapper;
import com.yvtian.pu.pojo.PuGoodsReceiptItemsEntity;
import com.yvtian.pu.pojo.PuOrderShopEntity;
import com.yvtian.pu.service.PuGoodsReceiptItemsService;
import com.yvtian.pu.service.PuGoodsReceiptService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购入库单(pu_goods_receipt)表服务实现类
 *
 * @author long
 * @since 2024-06-24 10:26:28
 */
@Service("puGoodsReceiptService")
public class PuGoodsReceiptServiceImpl extends ServiceImpl<PuGoodsReceiptMapper, PuGoodsReceiptEntity> implements PuGoodsReceiptService {

    @Autowired
    private PuGoodsReceiptItemsService puGoodsReceiptItemsService;
    @Autowired
    private ShopService shopService;

    @Override
    public IPage<PuGoodsReceiptEntity> findByPage(Page<PuGoodsReceiptEntity> page, PuGoodsReceiptEntity puGoodsReceiptEntity) {
        LambdaQueryWrapper<PuGoodsReceiptEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PuGoodsReceiptEntity::getIsDel, 0);
        getQueryWrapper(puGoodsReceiptEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(PuGoodsReceiptEntity puGoodsReceiptEntity) {
        ArrayList<PuGoodsReceiptItemsEntity> puGoodsReceiptItemList = puGoodsReceiptEntity.getPuGoodsReceiptItemList();
        if (puGoodsReceiptItemList != null && !puGoodsReceiptItemList.isEmpty()) {
            LambdaQueryWrapper<PuGoodsReceiptItemsEntity> shopLambdaQueryWrapper = new LambdaQueryWrapper<>();
            shopLambdaQueryWrapper.eq(PuGoodsReceiptItemsEntity::getZcode, puGoodsReceiptEntity.getCode());
            puGoodsReceiptItemsService.remove(shopLambdaQueryWrapper);
            puGoodsReceiptItemsService.saveBatch(puGoodsReceiptItemList);
        }
        this.saveOrUpdate(puGoodsReceiptEntity);
        return true;
    }

    @Transactional
    @Override
    public boolean handleStorage(PuGoodsReceiptEntity puGoodsReceiptEntity) {
        LambdaQueryWrapper<PuGoodsReceiptItemsEntity> itemQueryWrapper = new LambdaQueryWrapper<>();
        itemQueryWrapper.eq(PuGoodsReceiptItemsEntity::getZcode, puGoodsReceiptEntity.getCode());
        List<PuGoodsReceiptItemsEntity> goodsReceiptItemList = puGoodsReceiptItemsService.list(itemQueryWrapper);
        for (PuGoodsReceiptItemsEntity puGoodsReceiptItemsEntity : goodsReceiptItemList) {
            //更新商品库存
            LambdaUpdateWrapper<ShopEntity> shopEntityWrapper = new LambdaUpdateWrapper<>();
            if (BigDecimal.ZERO.compareTo(puGoodsReceiptItemsEntity.getQuantityReceived()) >= 0) {
                continue;
            }
            shopEntityWrapper.setSql("quantity = quantity + " + puGoodsReceiptItemsEntity.getQuantityReceived());
            shopEntityWrapper.eq(ShopEntity::getShopCode, puGoodsReceiptItemsEntity.getShopCode());
            shopService.update(shopEntityWrapper);
        }
        //更新采购入库单状态
        LambdaUpdateWrapper<PuGoodsReceiptEntity> puGoodsReceiptUpdateWrapper = new LambdaUpdateWrapper<>();
        puGoodsReceiptUpdateWrapper.eq(PuGoodsReceiptEntity::getCode, puGoodsReceiptEntity.getCode());
        puGoodsReceiptUpdateWrapper.set(PuGoodsReceiptEntity::getIsInbound, 1);
        this.update(puGoodsReceiptUpdateWrapper);
        return true;
    }

    private void getQueryWrapper(PuGoodsReceiptEntity puGoodsReceiptEntity, LambdaQueryWrapper<PuGoodsReceiptEntity> queryWrapper) {
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getCode())) {
            queryWrapper.like(PuGoodsReceiptEntity::getCode, puGoodsReceiptEntity.getCode());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getReceiptDateStr())) {
            String receiptDateStr = puGoodsReceiptEntity.getReceiptDateStr().trim();
            String[] split = receiptDateStr.split("~");
            queryWrapper.between(PuGoodsReceiptEntity::getReceiptDate, split[0], split[1]);
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getSupplierCode())) {
            queryWrapper.like(PuGoodsReceiptEntity::getSupplierCode, puGoodsReceiptEntity.getSupplierCode());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getSupplierName())) {
            queryWrapper.like(PuGoodsReceiptEntity::getSupplierName, puGoodsReceiptEntity.getSupplierName());
        }
        if (ObjectUtils.isNotEmpty(puGoodsReceiptEntity.getAmount())) {
            queryWrapper.eq(PuGoodsReceiptEntity::getAmount, puGoodsReceiptEntity.getAmount());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getOrderCode())) {
            queryWrapper.like(PuGoodsReceiptEntity::getOrderCode, puGoodsReceiptEntity.getOrderCode());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getOrderName())) {
            queryWrapper.like(PuGoodsReceiptEntity::getOrderName, puGoodsReceiptEntity.getOrderName());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getStockCode())) {
            queryWrapper.like(PuGoodsReceiptEntity::getStockCode, puGoodsReceiptEntity.getStockCode());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getStockName())) {
            queryWrapper.like(PuGoodsReceiptEntity::getStockName, puGoodsReceiptEntity.getStockName());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getProjectCode())) {
            queryWrapper.like(PuGoodsReceiptEntity::getProjectCode, puGoodsReceiptEntity.getProjectCode());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getProjectName())) {
            queryWrapper.like(PuGoodsReceiptEntity::getProjectName, puGoodsReceiptEntity.getProjectName());
        }
        if (puGoodsReceiptEntity.getIsInbound() != null) {
            queryWrapper.eq(PuGoodsReceiptEntity::getIsInbound, puGoodsReceiptEntity.getIsInbound());
        }
        if (ObjectUtils.isNotEmpty(puGoodsReceiptEntity.getStatus())) {
            queryWrapper.eq(PuGoodsReceiptEntity::getStatus, puGoodsReceiptEntity.getStatus());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getCreateName())) {
            queryWrapper.like(PuGoodsReceiptEntity::getCreateName, puGoodsReceiptEntity.getCreateName());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getUpdateName())) {
            queryWrapper.like(PuGoodsReceiptEntity::getUpdateName, puGoodsReceiptEntity.getUpdateName());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getCreateUser())) {
            queryWrapper.like(PuGoodsReceiptEntity::getCreateUser, puGoodsReceiptEntity.getCreateUser());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getRemarks())) {
            queryWrapper.like(PuGoodsReceiptEntity::getRemarks, puGoodsReceiptEntity.getRemarks());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getCheckName())) {
            queryWrapper.like(PuGoodsReceiptEntity::getCheckName, puGoodsReceiptEntity.getCheckName());
        }
        if (StringHelper.isNotEmpty(puGoodsReceiptEntity.getCheckUser())) {
            queryWrapper.like(PuGoodsReceiptEntity::getCheckUser, puGoodsReceiptEntity.getCheckUser());
        }
    }
}

